package Modes;

import java.util.ArrayList;
import java.util.List;

import Bacterias.Bac;
import Bacterias.Bacteria;
import Bacterias.Bacteria_Trap;
import GameElements.Coin;
import android.graphics.Canvas;
import android.util.Log;
import android.view.MotionEvent;

import com.game.ThauanFramework.GameObject;
import com.game.ThauanFramework.Util;
import com.game.ThauanFramework.TimeManager;
import com.thauanLopes.bacterium.GameSceneView;
import com.thauanLopes.bacterium.GameManages.GameManager;

public class NormalModeManager implements GameManager {

	private String TAG = "Normal Bacteria Manager";

	private boolean isAbleToTrash = false;
	private float minInterval = 0.8f;
	private float maxInterval = 1.2f;

	private float nextInterval;

	private float intervalUpDificult = 10;
	private float difucultIncrease = 0.2f;

	private boolean upDificultOn = true;
	private int contadorDificuldade = 0;

	private int dificultCount = 9;

	private float cronometroNovaGosma;
	private float lifeCronometer = 0;

	private List<Bac> bacterias;
	private List<GameObject> cemetery;
	private List<GameObject> gameObjects;

	private GameSceneView gameSceneView;
	private EspecialItemManager especialItemManager;

	// Bacteria Fatal
	private double bacFatal_cronometro;
	private float bacFatal_minIntervalo = 1;
	private float bacFatal_maxIntervalo = 10;
	private float bacFatal_currentIntervalo;

	// Bacteria Fatal
	public NormalModeManager(List<GameObject> cemetery,
			GameSceneView gameSceneView) {

		this.gameObjects = new ArrayList<GameObject>();
		this.bacterias = new ArrayList<Bac>();
		this.cemetery = cemetery;
		this.gameSceneView = gameSceneView;

		this.nextInterval = NextInterval(minInterval, maxInterval);

		this.especialItemManager = new EspecialItemManager(this,
				this.gameObjects, this.cemetery, this.gameSceneView);

		this.bacFatal_currentIntervalo = NextInterval(bacFatal_minIntervalo, bacFatal_maxIntervalo);
		
		Log.i(TAG, "Normal Bacteria Manager - instanciado");
	}

	@Override
	public void Update(double deltaTime) {

		// UPDATErs
		this.especialItemManager.Update(deltaTime);
		BacFatal_UpdateManager();

		// ============= INSTANTIATE BACTERIA ===========
		if (cronometroNovaGosma >= nextInterval) {

			Bacteria b = new Bacteria(bacterias, this.cemetery,
					this.gameSceneView);
			cronometroNovaGosma = 0;

			this.nextInterval = NextInterval(this.minInterval, this.maxInterval);

		} else {
			cronometroNovaGosma += deltaTime;
		}

		this.lifeCronometer += deltaTime;
		if (lifeCronometer > this.intervalUpDificult && this.upDificultOn) {

			this.minInterval -= this.difucultIncrease;

			if (minInterval < 0) {
				minInterval = 0;
			}

			this.maxInterval -= this.difucultIncrease;

			Log.i(TAG,
					String.valueOf(this.minInterval) + ", "
							+ String.valueOf(this.maxInterval));
			contadorDificuldade++;
			if (contadorDificuldade >= this.dificultCount) {
				upDificultOn = false;
			}

			lifeCronometer = 0;
		}

		// ============================================================
		// Destroy elements no more used
		if (!cemetery.isEmpty()) {

			if (cemetery.size() > 10) {
				for (GameObject gb : cemetery) {

					try {
						if(isAbleToTrash)
						gb.Destroy();
					} catch (Exception e) {
					}

					gb = null;
				}
			}
		}
		// cemetery.clear();
		// ===========================================================

		if (!this.bacterias.isEmpty()) {
			for (Bac bac : this.bacterias) {
				GameObject go_bac = (GameObject) bac;
				if (go_bac.isAlive)
					bac.Update(TimeManager.getInstance().getDeltaTime());

			}

			// ===========================================================

			if (!gameObjects.isEmpty()) {
				for (GameObject go : gameObjects) {
					if (go.isAlive)
						go.Update(TimeManager.getInstance().getDeltaTime());

				}
			}
		}
		// ----------------------- Coin
//		 if (this.cronometroCoin < this.coinChanceInterval) {
//		 this.cronometroCoin += deltaTime;
//		 }else {
//		 int coinLucky = MinhasCoisas.getRandom().nextInt(100);
//		 if (coinLucky >= 80) {
//		 Coin coin = new Coin(this.gameObjects, this.cemetery);
//		 cronometroCoin = 0;
//		 }
//		 }
		// -----------------------

	}

	// =======================================================================
	// DRAW
	@Override
	public void Draw(Canvas canvas) {

		if (!this.bacterias.isEmpty()) {
			for (Bac bac : this.bacterias) {
				GameObject go_bac = (GameObject) bac;
				if (go_bac.isAlive)
					bac.Draw(canvas);

			}
		}

		if (!gameObjects.isEmpty()) {
			for (GameObject go : gameObjects) {
				if (go.isAlive)
					go.Draw(canvas);

			}
		}
	}

	// ========================================================================
	// TOUCH
	@Override
	public boolean onTouch(MotionEvent event) {

		this.isAbleToTrash = false;
		try {

			if (!this.bacterias.isEmpty()) {
				for (Bac bac : this.bacterias) {
					GameObject go_bac = (GameObject) bac;
					if (go_bac.isAlive)
						bac.onTouch(event);
				}
			}
			if (!gameObjects.isEmpty()) {
				for (GameObject gb : gameObjects) {
					if (gb.isAlive)
						gb.onTouch(event);
				}
			}
		} catch (Exception e) {
			Log.i(TAG, "erro ao toque", e);
		}

		this.isAbleToTrash = true;
		return true;
	}

	private float NextInterval(float min, float max) {
		float interval = min + Util.getRandom().nextFloat()
				* (max - min);
		return interval;
	}

	private void BacFatal_UpdateManager() {
		this.bacFatal_cronometro += TimeManager.getInstance().getDeltaTime();
		if (this.bacFatal_cronometro > this.bacFatal_currentIntervalo) {
			this.bacFatal_cronometro = 0;
			this.bacFatal_currentIntervalo = NextInterval(
					this.bacFatal_minIntervalo, this.bacFatal_maxIntervalo);
			Bacteria_Trap trap = new Bacteria_Trap(bacterias, cemetery,
					this.gameSceneView);
		}
	}

	@Override
	public List<GameObject> getGos() {

		return this.gameObjects;
	}

}
